<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />



  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon.ico?v=5.1.4">







  <meta name="keywords" content="红宝书," />










<meta name="description" content="函数表达式定义函数的方式使用函数声明12345function 函数名(参数)&amp;#123;    //函数体&amp;#125;可通过name属性访问函数名（即标识符）（有何意义？）alert(函数名.name);//函数名 使用函数表达式这种情况下创建的函数叫匿名函数顾名思义，因为 function关键字后面没有函数名（即标识符）123var 变量=function()&amp;#123;    //函数体&amp;#">
<meta name="keywords" content="红宝书">
<meta property="og:type" content="article">
<meta property="og:title" content="函数表达式">
<meta property="og:url" content="http://yoursite.com/2018/05/03/函数表达式/index.html">
<meta property="og:site_name" content="Sivan">
<meta property="og:description" content="函数表达式定义函数的方式使用函数声明12345function 函数名(参数)&amp;#123;    //函数体&amp;#125;可通过name属性访问函数名（即标识符）（有何意义？）alert(函数名.name);//函数名 使用函数表达式这种情况下创建的函数叫匿名函数顾名思义，因为 function关键字后面没有函数名（即标识符）123var 变量=function()&amp;#123;    //函数体&amp;#">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2018-05-03T14:58:04.237Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="函数表达式">
<meta name="twitter:description" content="函数表达式定义函数的方式使用函数声明12345function 函数名(参数)&amp;#123;    //函数体&amp;#125;可通过name属性访问函数名（即标识符）（有何意义？）alert(函数名.name);//函数名 使用函数表达式这种情况下创建的函数叫匿名函数顾名思义，因为 function关键字后面没有函数名（即标识符）123var 变量=function()&amp;#123;    //函数体&amp;#">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2018/05/03/函数表达式/"/>





  <title>函数表达式 | Sivan</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  
	
  
	<script type="text/javascript" src="//cdn.bootcss.com/canvas-nest.js/1.0.0/canvas-nest.min.js"></script>
  
  
  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Sivan</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off"
             placeholder="搜索..." spellcheck="false"
             type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/05/03/函数表达式/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Sivan">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Sivan">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">函数表达式</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-05-03T22:35:50+08:00">
                2018-05-03
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/红宝书/" itemprop="url" rel="index">
                    <span itemprop="name">红宝书</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  2,323 字
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  9 分钟
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="函数表达式"><a href="#函数表达式" class="headerlink" title="函数表达式"></a>函数表达式</h1><h2 id="定义函数的方式"><a href="#定义函数的方式" class="headerlink" title="定义函数的方式"></a>定义函数的方式</h2><h4 id="使用函数声明"><a href="#使用函数声明" class="headerlink" title="使用函数声明"></a>使用函数声明</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">function 函数名(参数)&#123;</span><br><span class="line">    //函数体</span><br><span class="line">&#125;</span><br><span class="line">可通过name属性访问函数名（即标识符）（有何意义？）</span><br><span class="line">alert(函数名.name);//函数名</span><br></pre></td></tr></table></figure>
<h4 id="使用函数表达式"><a href="#使用函数表达式" class="headerlink" title="使用函数表达式"></a>使用函数表达式</h4><p>这种情况下创建的函数叫匿名函数<br>顾名思义，因为 function关键字后面没有函数名（即标识符）<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">var 变量=function()&#123;</span><br><span class="line">    //函数体</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure></p>
<h4 id="两者的主要区别"><a href="#两者的主要区别" class="headerlink" title="两者的主要区别"></a>两者的主要区别</h4><p>（1） 函数声明无论置于调用语句前后，解析器都会先读取函数声明  </p>
<blockquote>
<p>后面不能跟圆括号</p>
</blockquote>
<p>由于函数声明的一个重要特征—<em>函数声明提升</em>（在执行代码之前会先读取函数声明）这意味着可以把函数声明放在调用它的语句后面</p>
<p>（2）函数表达式则要等到解析器执行到它所在代码行才被真正执行</p>
<blockquote>
<p>后面可以跟圆括号，表示立即自我执行</p>
</blockquote>
<p>则函数表达式需要先把函数赋值给变量，再调用函数</p>
<h1 id="递归"><a href="#递归" class="headerlink" title="递归"></a>递归</h1><h3 id="递归函数"><a href="#递归函数" class="headerlink" title="递归函数"></a>递归函数</h3><p>是在一个函数通过名字调用自身的情况下构成的</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">function factorial(num)&#123;</span><br><span class="line">    if(num&lt;=1)&#123;</span><br><span class="line">        return 1;</span><br><span class="line">    &#125;else&#123;</span><br><span class="line">        return num*factorial(num-1);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>缺点：把该函数保存在另一个变量中，且将factorial变量设置为null时（即当函数名改变了时），导致错误</p>
<h3 id="改进"><a href="#改进" class="headerlink" title="改进"></a>改进</h3><p>arguments.callee是一个指向正在执行的函数的指针，通俗一点讲就是指向拥有arguments对象的函数</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">function factorial(num)&#123;</span><br><span class="line">    if(num&lt;=1)&#123;</span><br><span class="line">        return 1;</span><br><span class="line">    &#125;else&#123;          //arguments.callee代替函数名</span><br><span class="line">        return num*arguments.callee(num-1);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h1 id="闭包"><a href="#闭包" class="headerlink" title="闭包"></a>闭包</h1><h3 id="概念"><a href="#概念" class="headerlink" title="概念"></a>概念</h3><p>在讲述闭包之前必须先引入几个概念：<br><strong>函数的执行环境</strong>(excution context)、<strong>活动对</strong>象(call object)、<br><strong>作用域</strong>(scope)、<strong>作用域链</strong>(scope chain)<br>以函数a从定义到执行的过程为例阐述：</p>
<p>①  当<em>定义</em>函数a的时候，js解释器会将函数a的<strong>作用域链</strong>设置为定义a时a所在的“环境”，如果a是一个全局函数，则作用域链中只有全局变量对象。</p>
<p>②  当<em>执行</em>函数a的时候，a会进入相应的<strong>执行环境</strong>。  </p>
<p>③ 在创建执行环境的过程中，首先会为a添加一个scope属性，即a的<strong>作用域</strong>，其值就为第1步中作用域链。</p>
<p>④ 然后执行环境会创建一个<strong>活动对象</strong>（活动对象也是一个拥有属性的对象，但它不具有原型而且不能通过JavaScript代码直接访问）。创建完活动对象后，把活动对象添加到a的作用域链的<em>最顶端</em>。   </p>
<p>⑤下一步是在活动对象上添加一个<strong>arguments属性</strong>，它保存着调用函数a时所传递的参数。</p>
<p>⑥ 最后把<strong>所有函数a的形参和内部的函数b的引用</strong>也添加到a的活动对象上。在这一步中，完成了函数b的的定义，因此如同第3步，函数b的作用域链被设置为b所被定义的环境，即a的作用域。 </p>
<p>a的作用域链（本质上是一个指向变量对象的指针列表）包含</p>
<ul>
<li>本地活动对象：arguments属性，a的变量，函数</li>
<li>全局变量对象</li>
</ul>
<h3 id="小结"><a href="#小结" class="headerlink" title="小结"></a>小结</h3><ul>
<li>闭包是指有权访问另一个函数作用域中的变量的函数，通过模拟全局作用域，使局部变量变为全局变量<br>闭包就是将函数内部和函数外部连接起来的一座桥梁 </li>
</ul>
<p>创建闭包的常用方式：在一个函数内部创建另一个函数</p>
<ul>
<li>JavaScript中所有的function都是一个闭包。</li>
</ul>
<p>不过一般来说，嵌套的function所产生的闭包更为强大，也是大部分时候我们所谓的“闭包”。</p>
<ul>
<li>闭包的用途</li>
</ul>
<p>1、使其可以访问其外部函数作用域中的变量<br>2、可以实现局部变量驻留在内存中从而累加<br>通常函数的作用域及其所有变量会在函数执行结束后被销毁。  </p>
<p>但由于闭包（内部函数）的作用域链包含外部函数（非全局）的活动对象。当函数返回一个闭包时，函数的作用域将会一直在内存中保存到闭包不存在为止</p>
<ul>
<li>关于this对象</li>
</ul>
<p>闭包运行时this指向window</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">var name=&quot;The window&quot;;</span><br><span class="line">var object=&#123;</span><br><span class="line">    name:&quot;The Object&quot;,</span><br><span class="line">    getNameFunc:function()&#123;</span><br><span class="line">        return function()&#123;</span><br><span class="line">            return this.name; //这里的作用域，this指向window</span><br><span class="line">        &#125;;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line">alert(object.getNameFunc()()); //&quot;The window&quot;</span><br></pre></td></tr></table></figure>
<ol>
<li>改进1</li>
</ol>
<p>可适用对象冒充的方式使this指向object</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">alert(object.getNameFunc().call(object)); //对象冒充</span><br></pre></td></tr></table></figure>
<ol start="2">
<li>改进2  </li>
</ol>
<p>访问作用域中的this/arguments对象，必须将对该对象的引用（this）保存到另一个闭包能够访问的变量中</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">var name=&quot;The window&quot;;</span><br><span class="line">var object=&#123;</span><br><span class="line">    name:&quot;The Object&quot;,</span><br><span class="line">    getNameFunc:function()&#123;</span><br><span class="line">        var that=this;  //这里的作用域，this指向object</span><br><span class="line">        return function()&#123;</span><br><span class="line">            return that.name; </span><br><span class="line">        &#125;;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line">alert(object.getNameFunc()()); //&quot;The Object&quot;</span><br><span class="line">不过这个不会常用，太绕了！！！</span><br><span class="line">理解思路即可</span><br></pre></td></tr></table></figure>
<ol start="3">
<li>改进3  </li>
</ol>
<p>这不是闭包，也不算改进，只是通常是用这种来表示</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">var name=&quot;The window&quot;;</span><br><span class="line">var object=&#123;</span><br><span class="line">    name:&quot;The Object&quot;,</span><br><span class="line">    getNameFunc:function()&#123;</span><br><span class="line">            return this.name; </span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line">alert(object.getNameFunc()); //&quot;The Object&quot;</span><br></pre></td></tr></table></figure>
<h1 id="模仿块级作用域"><a href="#模仿块级作用域" class="headerlink" title="模仿块级作用域"></a>模仿块级作用域</h1><p>在JavaScript中，从变量有定义开始，就可以在函数内部随处访问它，即使错误地重新声明同一个变量，也不会改变它的值，当然，重新赋值是会改变的</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">(function()&#123;</span><br><span class="line">    //这里是块级作用域</span><br><span class="line">&#125;)();</span><br><span class="line"></span><br><span class="line">JavaScript将function关键字当作一个函数声明的开始，</span><br><span class="line">而函数声明后面不能跟圆括号，给函数声明加上一对圆括</span><br><span class="line">号即转换成函数表达式，后面就可以跟圆括号了</span><br></pre></td></tr></table></figure>
<p>使用块级作用域，匿名函数中定义的任何变量，都会在执行结束时被销毁<br>这种技术经常在全局作用域中被用在函数外部，从而限制向全局作用域中添加过多的变量和函数，能有效防止命名冲突</p>
<h1 id="私有变量"><a href="#私有变量" class="headerlink" title="私有变量"></a>私有变量</h1><p>严格来讲，JavaScript中没有私有成员的概念，所有对象属性都是公有的，不过有一个私有变量的概念<br>私有变量包括<strong>函数的参数</strong>、<strong>局部变量</strong>和<strong>在函数内部定义的其他函数</strong></p>
<ul>
<li><h3 id="特权方法"><a href="#特权方法" class="headerlink" title="特权方法"></a>特权方法</h3>有权访问私有变量的公有方法，在公共域可见，不可被修改，只能替换/删除</li>
</ul>
<blockquote>
<p>在这里提出一个问题：在函数外部私有变量不可访问，又为什么用特权方法使之可以访问呢？为什么不直接舍弃而用公有方法呢？<br>（个人理解）私有变量除了不可访问还不可修改，可隐藏那些不该被直接修改的数据，但私有变量在函数外部不可见，因此通过特权方法访问它们但也仅仅是访问而已</p>
</blockquote>
<ul>
<li><h3 id="创建特权方法的方式"><a href="#创建特权方法的方式" class="headerlink" title="创建特权方法的方式"></a>创建特权方法的方式</h3><h4 id="方法1"><a href="#方法1" class="headerlink" title="方法1"></a>方法1</h4>在构造函数中定义特权方法</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">function MyObject()&#123;</span><br><span class="line">     //私有变量和私有函数</span><br><span class="line">    var privateVariable=10;       </span><br><span class="line">    function privateFunction()&#123;   </span><br><span class="line">        return true;</span><br><span class="line">    &#125;</span><br><span class="line">    //特权方法</span><br><span class="line">    this.publicMethod=function()&#123;</span><br><span class="line">        privateVariable++;</span><br><span class="line">        return privateFunction();</span><br><span class="line">    &#125;;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="方法2"><a href="#方法2" class="headerlink" title="方法2"></a>方法2</h4><p>在私有作用域中定义私有变量和函数，通过构造函数的原型定义特权方法</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">(function()&#123;</span><br><span class="line">    //私有变量和私有函数</span><br><span class="line">    var privateVariable=10;       </span><br><span class="line">    function privateFunction()&#123;   </span><br><span class="line">        return true;</span><br><span class="line">    &#125;</span><br><span class="line">    //构造函数,在函数中不带var的变量为全局变量</span><br><span class="line">    MyObject=function()&#123;  &#125;;     //有点像原型模式创建对象</span><br><span class="line">    //公有/特权方法</span><br><span class="line">    MyObject.prototype.publicMethod=function()&#123;</span><br><span class="line">        privateVariable++;</span><br><span class="line">        return privateFunction();</span><br><span class="line">    &#125;;</span><br><span class="line">&#125;)();  //匿名函数的自我执行</span><br></pre></td></tr></table></figure>
<h4 id="方法3"><a href="#方法3" class="headerlink" title="方法3"></a>方法3</h4><p>模块模式<br>前两种方法是用于以<strong>构造函数的方式</strong>创建私有变量和特权方法的<br>这种是用于以<strong>对象字面量的方式</strong>为<em>单例</em>创建私有变量和特权方法的</p>
<ul>
<li>单例：只有一个实例的对象，其实为对象字面量声明方式，永远实例化一次</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">//以对象字面量的方式创建单例对象</span><br><span class="line">var singleton=&#123;</span><br><span class="line">    name:value,</span><br><span class="line">    method:function()&#123;</span><br><span class="line">        //这里是方法的代码</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">//为单例对象添加私有变量和特权方法使其增强</span><br><span class="line">var singleton=function()&#123;</span><br><span class="line">    //私有变量和私有函数</span><br><span class="line">    var privateVariable=10;       </span><br><span class="line">    function privateFunction()&#123;   </span><br><span class="line">        return true;</span><br><span class="line">    &#125;</span><br><span class="line">    //公有/特权方法和属性</span><br><span class="line">    return&#123;</span><br><span class="line">        publicProperty:true,</span><br><span class="line">        publicMethod:function()&#123;</span><br><span class="line">            privateVariable++;</span><br><span class="line">         return privateFunction();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;;</span><br><span class="line">&#125;();  //匿名函数的自我执行</span><br></pre></td></tr></table></figure>
<h4 id="方法4"><a href="#方法4" class="headerlink" title="方法4"></a>方法4</h4><p>增强的模块模式<br>在返回对象之前加入对其增强的代码<br>适合那些单例必须是某种类型的实例，同时还必须添加某些属性或方法对其加以增强的情况</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">var singleton=function()&#123;</span><br><span class="line">    //私有变量和私有函数</span><br><span class="line">    var privateVariable=10;       </span><br><span class="line">    function privateFunction()&#123;   </span><br><span class="line">        return true;</span><br><span class="line">    &#125;</span><br><span class="line">    //创建对象</span><br><span class="line">    var object=new CustomType();</span><br><span class="line">    //公有/特权方法和属</span><br><span class="line">        object.publicProperty:true,</span><br><span class="line">        object.publicMethod=function()&#123;</span><br><span class="line">            privateVariable++;</span><br><span class="line">         return privateFunction();</span><br><span class="line">        &#125;;</span><br><span class="line">    //返回这个对象</span><br><span class="line">    return object;</span><br><span class="line">&#125;();  //匿名函数的自我执行</span><br></pre></td></tr></table></figure>

      
    </div>
    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div></div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/images/wechatpay.jpg" alt="Sivan 微信支付"/>
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="/images/.jpg" alt="Sivan 支付宝"/>
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    <div>
      
        <div>
    
        <div style="text-align:center;color: #ccc;font-size:14px;">-------------本文结束<i class="fa fa-paw"></i>感谢您的阅读-------------</div>
    
</div>
      
    </div>

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/红宝书/" rel="tag"># 红宝书</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2018/05/03/JSON与Ajax/" rel="next" title="JSON与Ajax">
                <i class="fa fa-chevron-left"></i> JSON与Ajax
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2018/05/03/面向对象的程序设计/" rel="prev" title="面向对象的程序设计">
                面向对象的程序设计 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/images/avatar.jpg"
                alt="Sivan" />
            
              <p class="site-author-name" itemprop="name">Sivan</p>
              <p class="site-description motion-element" itemprop="description">I will be strong enough to make you feel bad</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">14</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">8</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">8</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/SivanWong" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://twitter.com/yourname" target="_blank" title="Twitter">
                      
                        <i class="fa fa-fw fa-twitter"></i>Twitter</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="http://weibo.com/your-user-name" target="_blank" title="微博">
                      
                        <i class="fa fa-fw fa-weibo"></i>微博</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#函数表达式"><span class="nav-number">1.</span> <span class="nav-text">函数表达式</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#定义函数的方式"><span class="nav-number">1.1.</span> <span class="nav-text">定义函数的方式</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#使用函数声明"><span class="nav-number">1.1.0.1.</span> <span class="nav-text">使用函数声明</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#使用函数表达式"><span class="nav-number">1.1.0.2.</span> <span class="nav-text">使用函数表达式</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#两者的主要区别"><span class="nav-number">1.1.0.3.</span> <span class="nav-text">两者的主要区别</span></a></li></ol></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#递归"><span class="nav-number">2.</span> <span class="nav-text">递归</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#递归函数"><span class="nav-number">2.0.1.</span> <span class="nav-text">递归函数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#改进"><span class="nav-number">2.0.2.</span> <span class="nav-text">改进</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#闭包"><span class="nav-number">3.</span> <span class="nav-text">闭包</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#概念"><span class="nav-number">3.0.1.</span> <span class="nav-text">概念</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#小结"><span class="nav-number">3.0.2.</span> <span class="nav-text">小结</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#模仿块级作用域"><span class="nav-number">4.</span> <span class="nav-text">模仿块级作用域</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#私有变量"><span class="nav-number">5.</span> <span class="nav-text">私有变量</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#特权方法"><span class="nav-number">5.0.1.</span> <span class="nav-text">特权方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#创建特权方法的方式"><span class="nav-number">5.0.2.</span> <span class="nav-text">创建特权方法的方式</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#方法1"><span class="nav-number">5.0.2.1.</span> <span class="nav-text">方法1</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#方法2"><span class="nav-number">5.0.2.2.</span> <span class="nav-text">方法2</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#方法3"><span class="nav-number">5.0.2.3.</span> <span class="nav-text">方法3</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#方法4"><span class="nav-number">5.0.2.4.</span> <span class="nav-text">方法4</span></a></li></ol></li></ol></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<div class="copyright">&copy; 2017 &mdash; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Sivan
  </span>

  
</div>


  <!-- <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div> -->
  <div class="powered-by">
  <i class="fa fa-user-md"></i><span id="busuanzi_container_site_uv">
    本站访客数:<span id="busuanzi_value_site_uv"></span>
  </span>
  </div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  











  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  

  
  
    <script type="text/javascript" src="/lib/canvas-nest/canvas-nest.min.js"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  

  
  

  

  

  

</body>
</html>
<script src="/js/src/love.js" type="text/javascript"></script>